dir("C:/Users/Rommel.Dks/Desktop/r")
install.packages ( "ggrepel")
install.packages( "ggplot2")
library(openxlsx)
library(dendextend)
library(ggplot2)
library(ggrepel)
library(ggplot2)
library(dendextend)
library(ggtree)

#1
options(scipen = 999)#关闭科学计数法
R4=read.xlsx("C:/Users/Rommel.Dks/Desktop/r/dataf.xlsx",sheet=1)
R41=as.matrix(R4[,2:65])
hist(as.matrix(R41))
hist(log2(as.matrix(R41)),main="汇总数据的直方图",labels=T)#汇总数据的直方图
#注以下出现的R3等为上次实习的4个data
R411=as.matrix(R3[,2:17])
R412=as.matrix(R31[,2:17])
R413=as.matrix(R32[,2:17])
R414=as.matrix(R33[,2:17])#处理，留下数值
hist(as.matrix(R411))
hist(log2(as.matrix(R411)),main="data1直方图",labels=T)
hist(as.matrix(R412))
hist(log2(as.matrix(R412)),main="data2直方图",labels=T)
hist(as.matrix(R413))
hist(log2(as.matrix(R413)),main="data3直方图",labels=T)
hist(as.matrix(R414))
hist(log2(as.matrix(R414)),main="data4直方图",labels=T)

#2
R42_matrix <- as.matrix(R41)
R421 <- hclust(dist(R42_matrix))# 进行层次聚类分析
plot(R421,main="ADdata树状图",cex = 0.7,bty = "7" )#设置边框没设置出来

#3
pacman::p_load(tidyverse, ggrepel)
RTH<- prostat
log10p <- c((-1)*log10(prostat[,3]))
RTH<- cbind(log10p, RTH)
cut_off_P=0.05
cut_off_log2FC =log2(1.2)#设置P与lo2FC的阈值
RTH$Sig = ifelse(RTH$P< cut_off_P &
                   abs (R431$FC) >= cut_off_log2FC,
                 ifelse(R431$FC > 0 ,'Up','Down'),'no')
ggplot(RTH,aes(FC, log10p))+
  geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "#999999")+
  geom_vline(xintercept = c(-log2(1.2),log2(1.2)), linetype = "dashed", color = "#999999")+
  geom_point(aes(color = Sig),
             size =1,
             alpha = 0.5) +
  theme_bw(base_size = 12)+
  ggsci::scale_color_jama() +
  theme(panel.grid = element_blank(),
        legend.position = 'right') +
  geom_text_repel(RTH = filter(RTH, abs(FC) > 1 & log10p > 38),
                  max.overlaps = getOption("ggrepel.max.overlaps", default = 20),
                  aes(label = ID, 
                      color = Sig),
                  size = 2) +
  xlab("log2 fold change")+
  ylab("-log10 P")
#上述学习于知乎https://zhuanlan.zhihu.com/p/628182802
ggsave("C:/Users/Rommel.Dks/Desktop/r/volcanoplot.jpg")

